﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;


namespace Unity聊天室_socket_服务器端
{
    class Program
    {
        public static Socket tcpServer;
        static List<Client> clientList = new List<Client>();
        public static void BroadcastMessage(string message)
        {
            var disconnectedList = new List<Client>();
            foreach (var client in clientList)
            {
                if (client.Connected)
                    client.SendMessages(message);
                else
                {
                    disconnectedList.Add(client);
                }
            }
            foreach(var temp in disconnectedList)
            {
                clientList.Remove(temp);
            }
        }
        static void Main(string[] args)
        {
             tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            string ipAddress = "192.168.0.112";
            tcpServer.Bind(new IPEndPoint(IPAddress.Parse(ipAddress),1559));
            tcpServer.Listen(100);
            Console.WriteLine("Connecting.....");
            while (true)
            {
                Socket tcpClient = tcpServer.Accept();
                Console.WriteLine("One client has connected!!");
                Client client = new Client(tcpClient);//把与每个客户端通信的逻辑放到Client类中做处理
                clientList.Add(client);
            }
             
        }
    }
}
